var prefix = "/jzweb/bookOrder";
$(function () {
    //学校查询
    $('#school').selectPage({
        showField: 'name',
        keyField: 'name',
        data: prefix + '/selectSchool',
        //向服务端提交的参数中，增加已选中的
        //设置返回-1，意为通知服务端返回空列表，初始化时使用
        params: function () {
            var pv = $('#school').val();
            return pv;
        },
        eAjaxSuccess: function (d) {
            var result;
            if (d) result = d.data;
            else result = undefined;
            return result;
        }
    });
    $('#school_text').val($('#school1s').val())
});

$().ready(function () {

    $("#bookId").val($("#bookIdNmae").val());

    $("#orderTypes").val($("#orderTypes11").val());

    $("#post").val($("#postHidden").val());
    $("#invoiceType").val($("#invoiceTypeHidden").val());
    $("#test").html(subFileName($("#exlUrl").val()));

    var type = $("#orderTypes").val();
    if (type != '') {

        if (type == 0) {  //为视频格式
            $("#schoolDiv").show();
            $("#schoolDiv1").hide();
        } else {  //为课件（ppt、pptx）格式
            $("#schoolDiv").hide();
            $("#schoolDiv1").show();
        }
    }
    validateRule();


});

$.validator.setDefaults({
    submitHandler: function () {
        update();
    }
});

function update() {

    $.ajax({
        cache: true,
        type: "POST",
        url: "/jzweb/bookOrder/updateYear2",
        data: $('#signupForm').serialize(),// 你的formid
        async: false,
        error: function (request) {
            parent.layer.alert("Connection error");
        },
        success: function (data) {
            if (data.code == 0) {
                parent.layer.msg("操作成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);

            } else {
                parent.layer.alert(data.msg)
            }

        }
    });

}

$("#orderNumber").blur(function () {
    sumPrice();
})

$("#discount").blur(function () {
    sumPrice();
})
$("#orderPrimary").blur(function () {
    sumPrice();
})
$("#discountPrimary").blur(function () {
    sumPrice();
})
$("#orderIntermediate").blur(function () {
    sumPrice();
})
$("#discountIntermediate").blur(function () {
    sumPrice();
})
$("#orderSenior").blur(function () {
    sumPrice();
})
$("#discountSenior").blur(function () {
    sumPrice();
})

function sumPrice() {

    if ($("#bookId").val() && $("#orderNumber").val() && $("#discount").val() && $("#orderPrimary").val() && $("#discountPrimary").val() && $("#orderIntermediate").val() && $("#discountIntermediate").val() && $("#orderSenior").val() && $("#discountSenior").val()) {

        $.ajax({
            cache: true,
            type: "get",
            url: "/jzweb/bookOrder/bookOrderSum",
            data: {
                bookId: $("#bookId").val(),
                orderNumber: $("#orderNumber").val(),
                discount: $("#discount").val(),
                orderPrimary: $("#orderPrimary").val(),
                discountPrimary: $("#discountPrimary").val(),
                orderIntermediate: $("#orderIntermediate").val(),
                discountIntermediate: $("#discountIntermediate").val(),
                orderSenior: $("#orderSenior").val(),
                discountSenior: $("#discountSenior").val()
            },// 你的formid
            async: false,
            error: function (request) {
                parent.layer.alert("Connection error");
            },
            success: function (data) {

                if (data.code == 0) {
                    $("#bookOrderSum").val(data.data.money);
                } else {
                    layer.alert(data.msg)
                }

            }
        });

    }

}

$.get(
    '/jzweb/examinationCost/userProvinceSel',
    {type: 0},
    function (data) {
        let str = '';
        let orpro = $("#orproCopy").val();
        if (data.code == 0) {
            str = `<option value="">--请选择省份--</option>`;
            data.data.forEach(function (v) {
                if (orpro == v.name) {
                    str += `<option value="${v.name}" selected>${v.name}</option>`
                }
                str += `<option value="${v.name}">${v.name}</option>`
            })
        }
        $("#orPro").html(str)
    }
)

$.get(
    '/jzweb/examinationCost/userProvinceSel',
    {type: 1},
    function (data) {
        let str = '';
        let orpro = $("#orcityCopy").val();
        if (data.code == 0) {
            str = `<option value="">--请选择城市--</option>`;
            data.data.forEach(function (v) {
                if (orpro == v.name) {
                    str += `<option value="${v.name}" selected>${v.name}</option>`
                }
                str += `<option value="${v.name}">${v.name}</option>`
            })
        }
        $("#orCity").html(str)
    }
)

layui.use('upload', function () {
    var upload = layui.upload;
    upload.render({
        elem: '#btnUploadOrder', //绑定元素
        url: '/common/sysFile/upload', //上传接口
        accept: 'file',
        done: function (r) {
            $("#exlUrl").val(r.data);
            $("#test").html(subFileName(r.data));
        },
        error: function (r) {
            layer.msg(r.msg);
        }
    });
});

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules: {
            bookId: {
                required: true
            },
            orderNumber: {
                required: true
            },
            orderPrimary: {
                required: true
            },
            orderIntermediate: {
                required: true
            },
            discount: {
                required: true
            },
            province: {
                required: true
            },
            address: {
                required: true
            },
            school: {
                required: true
            },
            addressee: {
                required: true
            },
            orProd: {
                required: true
            },
            orCityd: {
                required: true
            }

        },
        messages: {
            bookId: {
                required: icon + "请选择对应书籍种类"
            },
            orderNumber: {
                required: icon + "请输入理论订单数量"
            },
            orderPrimary: {
                required: icon + "请输入理论初级数量"
            },
            orderIntermediate: {
                required: icon + "请输入理论中级数量"
            },
            discount: {
                required: icon + "请输入折扣价"
            },
            province: {
                required: icon + "请输入省份"
            },
            address: {
                required: icon + "请输入详细地址"
            },
            school: {
                required: icon + "请输入单位名称或学校名称"
            },
            addressee: {
                required: icon + "请输入收件人"
            }
            , orProd: {
                required: icon + "请输入收件人"
            }
            , orCityd: {
                required: icon + "请输入收件人"
            }
        }
    })
}

$(function () {
    $("#orderTypes").change(function () {
        var type = $(this).val();
        if (type != '') {

            if (type == 0) {  //为视频格式
                $("#schoolDiv").show();
                $("#schoolDiv1").hide();
            } else {  //为课件（ppt、pptx）格式
                $("#schoolDiv").hide();
                $("#schoolDiv1").show();
            }
        }

    });


});


$(function () {
    //学校查询
    $('#examFeeIf').selectPage({

        showField: 'name',
        keyField: 'id',
        multiple: true,
        data: prefix + '/selectCost2?year=' + '2022',
        //向服务端提交的参数中，增加已选中的
        //设置返回-1，意为通知服务端返回空列表，初始化时使用
        params: function () {
            var pv = $('#examFeeIf').val();
            return pv;
        },
        eAjaxSuccess: function (d) {
            var result;
            if (d) result = d.data;
            else result = undefined;
            return result;
        }
    });

});

layui.use('laydate', function () {

    var laydate = layui.laydate;

    laydate.render({
        elem: '#orderCreatetime'
        , type: 'datetime'
    });

})